//
//  ViewController.m
//  DEMO3dTouh
//
//  Created by J on 16/6/14.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#define IOS_VERSION  [[[UIDevice currentDevice] systemVersion] floatValue]
#import "CollectionViewCell.h"
#import <objc/runtime.h>
#import "TodayKit.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerPreviewing>
@property(nonatomic,strong)UITableView* tableView;
@property(nonatomic,strong)UICollectionView* collectionView;

@property (strong, nonatomic) IBOutlet UILongPressGestureRecognizer *longPress;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 id obj = objc_getAssociatedObject(self, _cmd);
    NSLog(@"11%@",objc_getAssociatedObject(self, _cmd));

    if(!obj){
        objc_setAssociatedObject(self, _cmd, @"xxx", OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    
    NSLog(@"22%@",objc_getAssociatedObject(self, _cmd));
    // Do any additional setup after loading the view, typically from a nib.
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    [self.view addSubview:self.tableView];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    // 注册Peek和Pop方法
//    if (IOS_VERSION >= 9) {
//        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){
//            [self registerForPreviewingWithDelegate:self sourceView:self.view];
//        }
//    }
    
    
//    UICollectionViewLayout* layout = [UICollectionViewFlowLayout new];
//    UICollectionView* cole = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
//    
//    self.collectionView = cole;
//    [cole registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
//    cole.delegate = self;
//    cole.dataSource = self;
//
//    [self.view addSubview:cole];

    
    
    
    NSNumber* extensionValue = [sharedDefaults valueForKeyPath:@"slider"];
    self.sliderView.value = extensionValue.doubleValue;
    
    
    //app group路径
    
    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:shareKey];
    
    NSLog(@"app group:\n%@",containerURL.path);
    
    
    
    //打印可执行文件路径
    
    NSLog(@"bundle:\n%@",[[NSBundle mainBundle] bundlePath]);
    
    
    
    //打印documents
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *path = [paths objectAtIndex:0];
    
    NSLog(@"documents:\n%@",path);
    
    
    [self saveTextByNSFileManager];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    UIActivityViewController* activity = [[UIActivityViewController alloc] initWithActivityItems:@[self.imageView.image] applicationActivities:nil];
//    [self presentViewController:activity animated:YES completion:nil];
    
    [sharedDefaults setObject:@"image" forKey:@"image"];
    [sharedDefaults synchronize];
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UIView* cell = [collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"%@",cell);

}


- (BOOL)saveTextByNSFileManager {
    
    NSError *err = nil;
    

    NSString *string = @"http://img3.duitang.com/uploads/item/201605/04/20160504115210_TJW4M.thumb.700_0.jpeg";
    NSData *plistData = [string dataUsingEncoding:NSUTF8StringEncoding];
    
//    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:marr
//
//                                                                   format:NSPropertyListBinaryFormat_v1_0
//                         
//                                                         errorDescription:nil];
    
    
   BOOL result =  [plistData writeToURL:containerURL1 atomically:YES];//写入共享区文件

//    NSString *value = @(_sliderView.value).description;
//    BOOL result = [value writeToURL:containerURL atomically:YES encoding:NSUTF8StringEncoding error:&err];
    
    if (!result) {
        
        NSLog(@"%@",err);
        
    } else {
        
        NSLog(@"save value: success.");
        
    } 
    
    
    
    return result; 
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   
    
    return 100;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    id cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    
//        if (IOS_VERSION >= 9) {
//            if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){
            [self registerForPreviewingWithDelegate:self sourceView:cell];
//            }
//        }
    return cell;
    
}







#pragma mark - UIViewControllerPreviewingDelegate
#pragma mark peek手势
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    
//    UIGestureRecognizer* ges =  previewingContext.previewingGestureRecognizerForFailureRelationship;
    NSIndexPath* indexPath =  [self.collectionView indexPathForItemAtPoint:location];
    UIView*  cell = [self.collectionView cellForItemAtIndexPath:indexPath];

    NSLog(@"%@",cell.gestureRecognizers);
    //判断是否越界
    //设置选中size
    previewingContext.sourceRect = cell.frame;
    //返回显示页面
    UIViewController *vc = [UIViewController new];
    vc.view.backgroundColor = [UIColor whiteColor];
    return vc;
}

#pragma mark pop手势
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {

    [self showViewController:viewControllerToCommit sender:self];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [UITableViewCell new];
}

- (IBAction)slide:(UISlider*)sender forEvent:(UIEvent *)event {
    NSLog(@"%f",sender.value);
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CollectionViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@",cell);
    
    [cell performSelector: NSSelectorFromString(NSStringFromSelector(cell.action)) withObject:nil];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
